package com.yangxk.kadmin.generator.util;

import java.io.File;
import java.text.MessageFormat;

/**
 * 工具类
 * @author yangxk
 * @date 2018/10/4 20:55
 */
public class Tools {
    private Tools() {}

    public static String joinPakcage(String p1, String p2) {
        if (isEmpty(p1) || isEmpty(p2))
            throw new RuntimeException();
        return p1 + "." + p2;
    }

    public static String joinPath(String path1, String path2) {
        if (isEmpty(path1) || isEmpty(path2))
            throw new RuntimeException();
        return path1 + File.separator + path2;
    }

    public static String package2Path(String packageName) {
        if (packageName == null || packageName.isEmpty())
            return packageName;

        String replace = packageName.replace(".", File.separator);
        return replace;
    }

    public static String getProjectRootPath() {
        String classpath = Tools.class.getResource("/").getPath();
        File file = new File(classpath);
        return file.getParentFile().getParentFile().getAbsolutePath();
    }

    public static String relativeRootPath(String path) {
        if (path == null || path.isEmpty())
            return path;

        if (path.startsWith("/") || path.startsWith(File.separator))
            return getProjectRootPath() + path;

        return getProjectRootPath() + File.separator + path;
    }

    public static String getMavenJavaSrcPath() {
        return relativeRootPath("src/main/java");
    }

    public static String getMavenResourcesPath() {
        return relativeRootPath("src/main/resources");
    }

    public static String first2Lower(String str) {
        if (isEmpty(str))
            return str;
        if (Character.isLowerCase(str.charAt(0)))
            return str;
        return str.replaceFirst(new String(new char[]{str.charAt(0)}), new String(new char[]{Character.toLowerCase(str.charAt(0))}));
    }

    public static String first2Upper(String str) {
        if (isEmpty(str))
            return str;
        if (Character.isUpperCase(str.charAt(0)))
            return str;
        return str.replaceFirst(new String(new char[]{str.charAt(0)}), new String(new char[]{Character.toUpperCase(str.charAt(0))}));
    }


    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str))
            return true;
        return false;
    }

    public static String formatStr(String pattern, String...strings) {
        MessageFormat format = new MessageFormat(pattern);
        return format.format(strings);
    }

    // 从权限定名中获取类名称
    public static String getClassName(String packageName) {
        if (packageName == null || "".equals(packageName)) {
            return packageName;
        }
        return packageName.substring(packageName.lastIndexOf(".")+1);
    }

    public static void main(String[] args) {
        String str = "role";
        System.out.println(first2Upper(str));

        str = "/{0}/list";
        System.out.println(formatStr(str, "role"));
    }
}
